package com.LeeCode;

/**
 * 使数组互补的最少操作次数
 */

public class Code1674 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 2, 1};
        int limit = 2;
        System.out.println(new Code1674().minMoves(nums, limit));
    }

    public int minMoves(int[] nums, int limit) {
        int[] diff = new int[2 * limit + 2];
        int n = nums.length;
        for (int i = 0; i < n / 2; i++) {
            int p = Math.min(nums[i], nums[n - i - 1]), q = Math.max(nums[i], nums[n - i - 1]);
            diff[1 + p]++;
            diff[p + q]--;

            diff[p + q + 1]++;
            diff[limit + q + 1]--;

            if (p > 1) {
                diff[2] += 2;
                diff[1 + p] -= 2;
            }
            if (q < limit) {
                diff[limit + q + 1] += 2;
            }
        }

        int sum = 0, ans = n;
        for (int x = 2; x <= 2 * limit; x++) {
            sum += diff[x];
            ans = Math.min(ans, sum);
        }
        return ans;
    }
}
